/*
 * @(#)CookieNamePattern.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.web;

import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Web Platform Development Team
 */
public class CookieNamePattern implements CookiePattern {
	Log log = LogFactory.getLog(CookieNamePattern.class);

	private String patternStr;
	private String var;
	private Pattern pattern;

	/**
	 * Instantiates a new cookie name pattern.
	 * 
	 * @param var the var
	 * @param patternStr the pattern str
	 */
	public CookieNamePattern(String var, String patternStr) {
		this.var = var;
		this.patternStr = patternStr.trim();
		
		if (patternStr.indexOf("*") > -1) {
			log.debug("use wildcard character. pattern is " + patternStr);
			pattern = Pattern.compile("^" + patternStr.replaceAll("\\*", "[\\\\S]*") + "$");
		}
	}

	/**
	 * @see com.nhncorp.usf.core.web.CookiePattern#getVar()
	 * @return String 
	 */
	public String getVar() {
		return var;
	}

	/**
	 * @see com.nhncorp.usf.core.web.CookiePattern#isValidate(java.lang.String)
	 * @return boolean 
	 * @param input String
	 */
	public boolean isValidate(String input) {
		if (pattern != null) {
			return pattern.matcher(input).find();
		}
		
		return patternStr.equals(input.trim());
	}

	/**
	 * @see java.lang.Object#toString()
	 * @return String 
	 */
	@Override
	public String toString() {
		return patternStr;
	}

}
